home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 March / macformat-022.iso / Shareware City / Developers / src / out-of-phase-102-c / OutOfPhase 1.02 Source / OutOfPhase Folder / SymbolicPitch.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-11-23  |  9.2 KB  |  371 lines  |  [TEXT/KAHL]

  1. /* SymbolicPitch.c */
  2. /*****************************************************************************/
  3. /*                                                                           */
  4. /*    Out Of Phase:  Digital Music Synthesis on General Purpose Computers    */
  5. /*    Copyright (C) 1994  Thomas R. Lawrence                                 */
  6. /*                                                                           */
  7. /*    This program is free software; you can redistribute it and/or modify   */
  8. /*    it under the terms of the GNU General Public License as published by   */
  9. /*    the Free Software Foundation; either version 2 of the License, or      */
  10. /*    (at your option) any later version.                                    */
  11. /*                                                                           */
  12. /*    This program is distributed in the hope that it will be useful,        */
  13. /*    but WITHOUT ANY WARRANTY; without even the implied warranty of         */
  14. /*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          */
  15. /*    GNU General Public License for more details.                           */
  16. /*                                                                           */
  17. /*    You should have received a copy of the GNU General Public License      */
  18. /*    along with this program; if not, write to the Free Software            */
  19. /*    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.              */
  20. /*                                                                           */
  21. /*    Thomas R. Lawrence can be reached at tomlaw@world.std.com.             */
  22. /*                                                                           */
  23. /*****************************************************************************/
  24.  
  25. #include "MiscInfo.h"
  26. #include "Audit.h"
  27. #include "Debug.h"
  28. #include "Definitions.h"
  29.  
  30. #include "SymbolicPitch.h"
  31. #include "Frequency.h"
  32. #include "DataMunging.h"
  33. #include "Memory.h"
  34. #include "NoteObject.h"
  35. #include "Numbers.h"
  36. #include "SymbolicIsItInThere.h"
  37.  
  38.  
  39. /* convert a pitch number into a heap allocated string describing it */
  40. char*                        NumericPitchToString(short Pitch, unsigned long SharpFlatThing)
  41.     {
  42.         long                    Octave;
  43.         long                    NoteIndex;
  44.         char*                    NullTerminatedNoteSymbol;
  45.         char*                    OctaveNumber;
  46.         char*                    ReturnString;
  47.  
  48.         /* find out what octave */
  49.         Octave = (Pitch / 12);
  50.         NoteIndex = Pitch % 12;
  51.  
  52.         /* figure out what note symbol to use */
  53.         switch (NoteIndex)
  54.             {
  55.                 default:
  56.                     EXECUTE(PRERR(ForceAbort,"NumericPitchToString:  bizarre % error"));
  57.                     break;
  58.                 case 0: /* B#/C */
  59.                     if ((SharpFlatThing & eSharpModifier) != 0)
  60.                         {
  61.                             NullTerminatedNoteSymbol = "B sharp ";
  62.                         }
  63.                     else
  64.                         {
  65.                             NullTerminatedNoteSymbol = "C ";
  66.                         }
  67.                     break;
  68.                 case 1: /* C#/Db */
  69.                     if ((SharpFlatThing & eSharpModifier) != 0)
  70.                         {
  71.                             NullTerminatedNoteSymbol = "C sharp ";
  72.                         }
  73.                      else
  74.                         {
  75.                             NullTerminatedNoteSymbol = "D flat ";
  76.                         }
  77.                     break;
  78.                 case 2: /* D */
  79.                     NullTerminatedNoteSymbol = "D ";
  80.                     break;
  81.                 case 3: /* D#/Eb */
  82.                     if ((SharpFlatThing & eSharpModifier) != 0)
  83.                         {
  84.                             NullTerminatedNoteSymbol = "D sharp ";
  85.                         }
  86.                      else
  87.                         {
  88.                             NullTerminatedNoteSymbol = "E flat ";
  89.                         }
  90.                     break;
  91.                 case 4: /* E/Fb */
  92.                     if ((SharpFlatThing & eFlatModifier) != 0)
  93.                         {
  94.                             NullTerminatedNoteSymbol = "F flat ";
  95.                         }
  96.                      else
  97.                         {
  98.                             NullTerminatedNoteSymbol = "E ";
  99.                         }
  100.                     break;
  101.                 case 5: /* E#/F */
  102.                     if ((SharpFlatThing & eSharpModifier) != 0)
  103.                         {
  104.                             NullTerminatedNoteSymbol = "E sharp ";
  105.                         }
  106.                      else
  107.                         {
  108.                             NullTerminatedNoteSymbol = "F ";
  109.                         }
  110.                     break;
  111.                 case 6: /* F#/Gb */
  112.                     if ((SharpFlatThing & eSharpModifier) != 0)
  113.                         {
  114.                             NullTerminatedNoteSymbol = "F sharp ";
  115.                         }
  116.                      else
  117.                         {
  118.                             NullTerminatedNoteSymbol = "G flat ";
  119.                         }
  120.                     break;
  121.                 case 7: /* G */
  122.                     NullTerminatedNoteSymbol = "G ";
  123.                     break;
  124.                 case 8: /* G#/Ab */
  125.                     if ((SharpFlatThing & eSharpModifier) != 0)
  126.                         {
  127.                             NullTerminatedNoteSymbol = "G sharp ";
  128.                         }
  129.                      else
  130.                         {
  131.                             NullTerminatedNoteSymbol = "A flat ";
  132.                         }
  133.                     break;
  134.                 case 9: /* A */
  135.                     NullTerminatedNoteSymbol = "A ";
  136.                     break;
  137.                 case 10: /* A#/Bb */
  138.                     if ((SharpFlatThing & eSharpModifier) != 0)
  139.                         {
  140.                             NullTerminatedNoteSymbol = "A sharp ";
  141.                         }
  142.                      else
  143.                         {
  144.                             NullTerminatedNoteSymbol = "B flat ";
  145.                         }
  146.                     break;
  147.                 case 11: /* B/Cb */
  148.                     if ((SharpFlatThing & eFlatModifier) != 0)
  149.                         {
  150.                             NullTerminatedNoteSymbol = "C flat ";
  151.                         }
  152.                      else
  153.                         {
  154.                             NullTerminatedNoteSymbol = "B ";
  155.                         }
  156.                     break;
  157.             }
  158.  
  159.         ReturnString = NIL;
  160.  
  161.         /* figure out octave */
  162.         OctaveNumber = IntegerToString(Octave - (CENTERNOTE / 12));
  163.         if (OctaveNumber != NIL)
  164.             {
  165.                 char*                    SymbolNoNull;
  166.  
  167.                 SymbolNoNull = StringToBlockCopy(NullTerminatedNoteSymbol);
  168.                 if (SymbolNoNull != NIL)
  169.                     {
  170.                         ReturnString = ConcatBlockCopy(SymbolNoNull,OctaveNumber);
  171.                         if (ReturnString != NIL)
  172.                             {
  173.                                 SetTag(ReturnString,"NumericPitchToString");
  174.                             }
  175.                         ReleasePtr(SymbolNoNull);
  176.                     }
  177.                 ReleasePtr(OctaveNumber);
  178.             }
  179.  
  180.         return ReturnString;
  181.     }
  182.  
  183.  
  184. /* convert the string into a pitch and sharp/flat word. */
  185. void                        StringToNumericPitch(char* String, short* Pitch,
  186.                                     unsigned long* SharpFlatThing)
  187.     {
  188.         long                    LocalPitch;
  189.         long                    Octave;
  190.         long                    Index;
  191.  
  192.         CheckPtrExistence(String);
  193.         LocalPitch = (*Pitch) % 12; /* get the default */
  194.         Octave = (*Pitch) / 12; /* defaults */
  195.  
  196.         if (PtrSize(String) == 0)
  197.             {
  198.                 /* return without changing anything */
  199.                 return;
  200.             }
  201.  
  202.         /* check the pitch specifier */
  203.         switch (String[0])
  204.             {
  205.                 default:
  206.                     break;
  207.                 case 'a': case 'A':
  208.                     if (IsItInThere(String,"sharp") || IsItInThere(String,"#"))
  209.                         {
  210.                             /* A# */
  211.                             LocalPitch = 9 + 1;
  212.                             *SharpFlatThing = eSharpModifier;
  213.                         }
  214.                     else if (IsItInThere(String,"flat"))
  215.                         {
  216.                             /* Ab */
  217.                             LocalPitch = 9 - 1;
  218.                             *SharpFlatThing = eFlatModifier;
  219.                         }
  220.                     else
  221.                         {
  222.                             /* A */
  223.                             LocalPitch = 9;
  224.                             *SharpFlatThing = 0;
  225.                         }
  226.                     break;
  227.                 case 'B': case 'b':
  228.                     if (IsItInThere(String,"sharp") || IsItInThere(String,"#"))
  229.                         {
  230.                             /* B# */
  231.                             LocalPitch = 11 + 1;
  232.                             *SharpFlatThing = eSharpModifier;
  233.                         }
  234.                     else if (IsItInThere(String,"flat"))
  235.                         {
  236.                             /* Bb */
  237.                             LocalPitch = 11 - 1;
  238.                             *SharpFlatThing = eFlatModifier;
  239.                         }
  240.                     else
  241.                         {
  242.                             /* B */
  243.                             LocalPitch = 11;
  244.                             *SharpFlatThing = 0;
  245.                         }
  246.                     break;
  247.                 case 'C': case 'c':
  248.                     if (IsItInThere(String,"sharp") || IsItInThere(String,"#"))
  249.                         {
  250.                             /* C# */
  251.                             LocalPitch = 0 + 1;
  252.                             *SharpFlatThing = eSharpModifier;
  253.                         }
  254.                     else if (IsItInThere(String,"flat"))
  255.                         {
  256.                             /* Cb */
  257.                             LocalPitch = 0 - 1 + 12;
  258.                             *SharpFlatThing = eFlatModifier;
  259.                         }
  260.                     else
  261.                         {
  262.                             /* C */
  263.                             LocalPitch = 0;
  264.                             *SharpFlatThing = 0;
  265.                         }
  266.                     break;
  267.                 case 'D': case 'd':
  268.                     if (IsItInThere(String,"sharp") || IsItInThere(String,"#"))
  269.                         {
  270.                             /* D# */
  271.                             LocalPitch = 2 + 1;
  272.                             *SharpFlatThing = eSharpModifier;
  273.                         }
  274.                     else if (IsItInThere(String,"flat"))
  275.                         {
  276.                             /* Db */
  277.                             LocalPitch = 2 - 1;
  278.                             *SharpFlatThing = eFlatModifier;
  279.                         }
  280.                     else
  281.                         {
  282.                             /* D */
  283.                             LocalPitch = 2;
  284.                             *SharpFlatThing = 0;
  285.                         }
  286.                     break;
  287.                 case 'E': case 'e':
  288.                     if (IsItInThere(String,"sharp") || IsItInThere(String,"#"))
  289.                         {
  290.                             /* E# */
  291.                             LocalPitch = 4 + 1;
  292.                             *SharpFlatThing = eSharpModifier;
  293.                         }
  294.                     else if (IsItInThere(String,"flat"))
  295.                         {
  296.                             /* Eb */
  297.                             LocalPitch = 4 - 1;
  298.                             *SharpFlatThing = eFlatModifier;
  299.                         }
  300.                     else
  301.                         {
  302.                             /* E */
  303.                             LocalPitch = 4;
  304.                             *SharpFlatThing = 0;
  305.                         }
  306.                     break;
  307.                 case 'F': case 'f':
  308.                     if (IsItInThere(String,"sharp") || IsItInThere(String,"#"))
  309.                         {
  310.                             /* F# */
  311.                             LocalPitch = 5 + 1;
  312.                             *SharpFlatThing = eSharpModifier;
  313.                         }
  314.                     else if (IsItInThere(String,"flat"))
  315.                         {
  316.                             /* Fb */
  317.                             LocalPitch = 5 - 1;
  318.                             *SharpFlatThing = eFlatModifier;
  319.                         }
  320.                     else
  321.                         {
  322.                             /* F */
  323.                             LocalPitch = 5;
  324.                             *SharpFlatThing = 0;
  325.                         }
  326.                     break;
  327.                 case 'G': case 'g':
  328.                     if (IsItInThere(String,"sharp") || IsItInThere(String,"#"))
  329.                         {
  330.                             /* G# */
  331.                             LocalPitch = 7 + 1;
  332.                             *SharpFlatThing = eSharpModifier;
  333.                         }
  334.                     else if (IsItInThere(String,"flat"))
  335.                         {
  336.                             /* Gb */
  337.                             LocalPitch = 7 - 1;
  338.                             *SharpFlatThing = eFlatModifier;
  339.                         }
  340.                     else
  341.                         {
  342.                             /* G */
  343.                             LocalPitch = 7;
  344.                             *SharpFlatThing = 0;
  345.                         }
  346.                     break;
  347.             }
  348.  
  349.         /* try to figure out what octave they want */
  350.         Index = 0;
  351.         while ((Index < PtrSize(String)) && (String[Index] != '-')
  352.             && ((String[Index] < '0') || (String[Index] > '9')))
  353.             {
  354.                 Index += 1;
  355.             }
  356.         if (Index < PtrSize(String))
  357.             {
  358.                 Octave = StringToInteger(&(String[Index]),PtrSize(String) - Index);
  359.             }
  360.         if ((Octave * 12) + CENTERNOTE < 0)
  361.             {
  362.                 Octave = - CENTERNOTE / 12;
  363.             }
  364.         if ((Octave * 12) + CENTERNOTE > NUMNOTES - 1)
  365.             {
  366.                 Octave = CENTERNOTE / 12 - 1;
  367.             }
  368.  
  369.         *Pitch = ((Octave + (CENTERNOTE / 12)) * 12) + LocalPitch;
  370.     }
  371.